<template>
  <!-- 系统管理 -->
  <div class="app-container">
    <div class="mainbox">
      <!-- 顶部操作 -->
      <div class="filter-container">
        <el-form ref="filterFormRef" :inline="true" :model="filterForm">
          <el-form-item label="企业名称:" prop="companiesName">
            <el-input
              v-model.trim="filterForm.companiesName"
              class="filter-item limit"
              style="width: 200px;margin-right:10px"
              maxlength="30"
              clearable
              placeholder="请输入企业名称"
              @keyup.enter.native="$_handleFilter"
            />
          </el-form-item>
          <el-form-item label="企业类型:" prop="companiesCode">
            <el-select v-model.trim="filterForm.companiesCode" multiple placeholder="请选择" :clearable="true">
              <el-option
                v-for="dict in dict.type.QYLX"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="活动类型:" prop="eventsTypeCode">
            <el-select v-model.trim="filterForm.eventsTypeCode" placeholder="请选择" :clearable="true">
              <el-option
                v-for="dict in dict.type.zs_hdlx"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="时间范围:" prop="times">
            <el-date-picker
              v-model="filterForm.times"
              type="datetimerange"
              :picker-options="pickerOptions"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd HH:mm:ss"
              align="right">
            </el-date-picker>
          </el-form-item>
          <el-button type="primary" @click="$_handleFilter">查询</el-button>
          <el-button @click="$_handleReset">重置</el-button>
        </el-form>

      </div>
      <!-- 表格 -->
      <div class="table-container table-fullscreen">
        <div class="table-opt-container">
          <el-button
            v-permission="['INVESTMENTACTIVITY_ADD_BUTTON']"
            type="primary"
            icon="el-icon-plus"
            @click="handleOpera()"
          >
            新增
          </el-button>
          <el-button
            v-permission="['INVESTMENTACTIVITY_DOWNLOAD_BUTTON']"
            icon="el-icon-download"
            @click="handleExport('d')"
          >
            下载模板
          </el-button>
          <el-button
            v-permission="['INVESTMENTACTIVITY_IMPORT_BUTTON']"
            icon="el-icon-folder-add"
            type="primary"
            @click="handleOpera"
          >
            导入
          </el-button>
          <el-button
            v-permission="['INVESTMENTACTIVITY_EXPORT_BUTTON']"
            icon="el-icon-folder-add"
            type="primary"
            @click="handleExport()"
          >
            导出活动台账
          </el-button>
          <el-button
            v-permission="['INVESTMENTACTIVITY_EXPORT_YQ_BUTTON']"
            icon="el-icon-folder-add"
            type="primary"
            @click="handleExport('yq')"
          >
            导出央企活动台账
          </el-button>
          <div class="flex-1" />
          <dt-dialog-column :columns="columns" @queryTable="getList" />
        </div>
        <el-table
          ref="table"
          v-loading="MX_listLoading"
          :data="MX_list"
          border
          highlight-current-row
          :row-key="$_getRowKey"
          style="width: 100%;text-align:center"
          :header-cell-style="MX_headerCellStyle"
        >
          <template slot="empty">
            <p>{{ $store.getters.dataText }}</p>
          </template>
          <el-table-column
            fixed="left"
            type="index"
            label="序号"
            width="70"
            align="center"
            :index="$_tableIndex"
          />
          <el-table-column
            v-for="item in columns"
            :key="item.prop"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            :min-width="item.minWidth"
            :formatter="item.formatter"
            align="center"
          />
          <el-table-column
            :key="Math.random()"
            fixed="right"
            label="操作"
            width="220px"
            align="center"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                class="tBtn"
                @click="$_handleDetail(scope.row, getDetailApi, idKeyName)"
              >详情</el-button>
              <el-button
                v-permission="['INVESTMENTACTIVITY_UPDATE_BUTTON']"
                size="mini"
                class="tBtn"
                @click="handleEdit(scope.row)"
              >修改</el-button>
              <el-button
                v-permission="['INVESTMENTACTIVITY_DEL_BUTTON']"
                size="mini"
                type="danger"
                class="tBtn"
                @click="handleDelete(scope.row[idKeyName])"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <dt-pagination
          v-show="MX_total > 0"
          :total="MX_total"
          :page.sync="MX_pageNav.pageNo"
          :limit.sync="MX_pageNav.pageSize"
          @pagination="getList"
        />
      </div>
      <DialogForm ref="dialogForm" labelWidth="140px" :formDesignList="formDesignList" />
      <dtDialog
        title="导入"
        class="get-page-form-import-dialog"
        :visible.sync="MX_fileUploadVisible"
        width="620px"
        :is-loading="MX_uploadLoading"
        @closeDialog="MX_fileUploadVisible = false"
        @comfirmBtn="$_importDataSave(importApi)"
      >
        <el-form slot="content">
          <div class="">
            <dt-importFile ref="dtImportFileRef" :down-load-template="handleImportTemplate" :type="fileName" />
          </div>
        </el-form>
      </dtDialog>
    </div>
  </div>
</template>
<script>
import DialogForm from '@/views/investment/components/DialogForm'
import commonList from "@/utils/mixins/investment/commonList";
import intservApi from "@/api/investment/intserv";
import systemApi from "@/api/investment/system";
import dataCaptureApi from "@/api/investment/dataCapture";

export default {
  name: 'InvestmentActivity',
  components: {
    DialogForm
  },
  dicts: ['QYLX', 'zs_hdlx'],
  mixins: [commonList],
  data() {
    const _this = this
    return {
      filterForm: {
        companiesName: '',
        companiesCode: '',
        eventsTypeCode: '',
        times: ''
      },
      fileName: '招商活动数据导入',
      handleImportTemplate: intservApi.downloadInvestmentActivity,
      importApi: intservApi.importInvestmentActivity,
      getDetailApi: intservApi.detailInvestmentActivityItem,
      // 列信息
      columns: [
        { prop: 'dedicatedName', label: `专班名称`, width: '180px', formatter: (row) => {
            let item = _this.zbList.filter(v => v.value === row.dedicatedId)
            return item.length ? item[0].label : ''
          } },
        { prop: 'leadsId', label: `关联线索`, width: '180px', formatter: (row) => {
            let item = _this.xsList.filter(v => v.value == row.leadsId)
            return item.length ? item[0].label : ''
          } },
        { prop: 'eventsTime', label: `时间`, width: '180px' },
        { prop: 'location', label: `地点`, minWidth: '200px' },
        { prop: 'eventsTypeCode', label: `活动类型`, width: '140px', formatter: function(row) {
            return _this.dict.label.zs_hdlx[row.eventsTypeCode]
          } },
        { prop: 'companiesCode', label: `企业类型`, width: '140px', formatter: function(row) {
          let arr = []
            if (!!row.companiesCode){
              row.companiesCode.map(v => {
                arr.push(_this.dict.label.QYLX[v])
              })
            }
            return arr.join('-')
          } },
        { prop: 'companiesName', label: `企业名称`, minWidth: '180px' },
        { prop: 'project', label: `洽谈项目`, minWidth: '180px' },
        { prop: 'department', label: `牵头部门`, width: '180px' },
        { prop: 'accompanied', label: `陪同人员`, minWidth: '180px' }
      ],
      formDesignList: [
        { type: 'select', prop: 'dedicatedId', label: `专班`, readonly: true, rules: [{ required: true, message: '请选择专班', trigger: 'change' }] },
        { type: 'datetime', prop: 'eventsTime', label: `时间`, format: 'yyyy-MM-dd HH:mm:ss', rules: [{ required: true, message: '请选择时间', trigger: 'blur' }] },
        { type: 'text', prop: 'location', label: `地点`, rules: [{ required: true, message: '请输入地点', trigger: 'blur' }] },
        { type: 'select', prop: 'leadsId', label: `关联线索` },
        { type: 'select', prop: 'eventsTypeCode', label: `活动类型`, dictTypeCode: 'zs_hdlx', rules: [{ required: true, message: '请选择活动类型', trigger: 'blur' }] },
        { type: 'select', prop: 'companiesCode', label: `企业类型`, dictTypeCode: 'QYLX', multiple: true, rules: [{ required: true, message: '请选择企业类型', trigger: 'change' }] },
        { type: 'text', prop: 'companiesName', label: `企业名称`, rules: [{ required: true, message: '请输入企业名称', trigger: 'blur' }] },
        { type: 'text', prop: 'project', label: `洽谈项目`, rules: [{ required: true, message: '请输入洽谈项目', trigger: 'blur' }] },
        { type: 'text', prop: 'department', label: `牵头部门`, rules: [{ message: '请输入牵头部门', trigger: 'blur' }] },
        { type: 'text', prop: 'accompanied', label: `陪同人员`, rules: [{ message: '请输入陪同人员', trigger: 'blur' }] },
        { type: 'file', prop: 'annex', label: `附件`, fileSize: 15, fileTypes: ['doc', 'docx', 'xlsx', 'xls', 'ppt', 'txt', 'pdf'], rules: [{ message: '请上传附件', trigger: 'change' }] },
        { type: 'textarea', prop: 'eventsContent', label: `内容`, maxlength: 500, autosize: {minRows: 5, maxRows: 10}, rules: [{ message: '请输入内容', trigger: 'blur' }] },
        { type: 'textarea', prop: 'remark', label: `备注`, maxlength: 500, autosize: {minRows: 5, maxRows: 10}, rules: [{ message: '请输入备注', trigger: 'blur' }] },
      ],
      idKeyName: 'eventsId',
      zbList: [],
      xsList: [],
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      userZBInfo: {},
    }
  },
  watch: {
    'filterForm.times'(val){
      this.$set(this.filterForm, 'searchBeginTime', !!val ? val[0] : '')
      this.$set(this.filterForm, 'searchEndTime', !!val ? val[1] : '')
    },
    zbList(val){
      this.$set(this.formDesignList[0], 'dictOptions', val)
    },
    xsList(val){
      this.$set(this.formDesignList[3], 'dictOptions', val)
    }
  },
  created() {
    this.getList()
    this.getZBList()
    this.getUserZBInfo()
    this.getXSList()
  },
  activated() {
  },
  methods: {
    getUserZBInfo(){
      dataCaptureApi.getUserOfZB().then(res => {
        this.userZBInfo = res.data
        if(!!this.userZBInfo.specializedId) {
          this.$set(this.formDesignList[0], 'defaultVal', this.userZBInfo.specializedId)
        }
      })
    },
    getZBList(){
      systemApi.getZBList({pageSize: -1}).then(res => {
        let list = res.data.rows || [];
        this.zbList = list.map(v => {
          v.value = v.id
          v.label = v.groupName
          return v
        })
      })
    },
    getXSList(){
      dataCaptureApi.getXSList({status: 1, pageSize: -1, pageNo: 1}).then(res => {
        let list = res.data.records || [];
        this.xsList = list.map(v => {
          v.value = v.id
          v.label = v.companyName
          return v
        })
      })
    },
    // 获取页面列表
    getList() {
      let filterForm = JSON.parse(JSON.stringify(this.filterForm))
      filterForm.companiesCode = !!filterForm.companiesCode ? filterForm.companiesCode : []
      delete filterForm.times
      this.$_getList(intservApi.getInvestmentActivityList,{
        ...filterForm,
        ...this.MX_pageNav
      })
    },
    // 编辑按钮
    handleEdit(data) {
      this.$_handleDetail(data, this.getDetailApi, this.idKeyName, (res) => {
        this.$refs.dialogForm.open('update', intservApi.updateInvestmentActivityItem, res || this.MX_applicationMsg)
      })
    },
    // 添加按钮
    handleAdd() {
      if(!this.userZBInfo.specializedId) {
        this.$dtMessage({type: 'error', message: '当前用户不为专班成员，不可新增！'})
        return
      }
      this.$refs.dialogForm.open('add', intservApi.addInvestmentActivityItem)
    },
    // 删除按钮
    handleDelete(id) {
      this.$_handleDelete(intservApi.delInvestmentActivityItem, [id], `${this.idKeyName}s`)
    },
    /** 导出/下载按钮操作 */
    handleExport(type = 'export') {
      if (type === 'export'){
        this.$_handleDownload(intservApi.exportInvestmentActivity, '招商活动台账', this.filterForm, '导出');
      }else if (type === 'yq'){
        this.$_handleDownload(intservApi.exportInvestmentActivityYQ, '招商央企活动台账', this.filterForm, '导出');
      }else{
        this.$_handleDownload(this.handleImportTemplate, `${this.fileName}模板`);
      }
    },
    handleOpera(type = 'add'){
      let tips = type === 'add' ? '新增' : '导入'
      if(!this.userZBInfo.specializedId) {
        this.$dtMessage({type: 'error', message: `当前用户不为专班成员，不可${tips}！`})
        return
      }
      if (type === 'add'){
        this.$refs.dialogForm.open('add', intservApi.addInvestmentActivityItem)
      }else{
        this.$_handleImport()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tBtn{
  margin-top: 3px;
  margin-bottom: 3px;
}
</style>
